package com.quzhi;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

public class CodeGenerator {
    public static void main(String[] args) {
//        String url = "jdbc:mysql://154.8.182.144:3306/qu_zhi?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
//        String username = "qu_zhi";
//        String password = "qu_zhi";
        String url = "jdbc:mysql:///qu_zhi";
        String username = "root";
        String password = "123";



        String moduleName = "sys";
        String mapperLocation = "D:\\DeskTop\\QuZhi\\quzhi_java\\quzhi_back\\src\\main\\resources\\mapper\\" + moduleName;

//        String tables = "qz_user,qz_order,qz_order_serial_number,qz_order_remark,qz_receive_goods,qz_goods_sign,qz_role,qz_user_role,qz_product,qz_inventory,qz_warehouse,qz_goods_type,qz_transfer_record,qz_transfer_goods,qz_goods_record,qz_add_goods,qz_order_log";
//        String tables = "qz_goods_serial_number";
//        String tables = "qz_blacklist";
//        String tables = "qz_menu,qz_role_menu";
        String tables = "qz_notice";
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("Xibing") // 设置作者
                            //.enableSwagger() // 开启 swagger 模式
                            //.fileOverride() // 覆盖已生成文件
                            .outputDir("D:\\DeskTop\\QuZhi\\quzhi_java\\quzhi_back\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.quzhi") // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, mapperLocation)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables) // 设置需要生成的表名
                            .addTablePrefix("qz_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
